package com.itnetwork.Action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.TransformerException;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.itnetwork.Entity.Category;
import com.itnetwork.Entity.News;
import com.itnetwork.ServiceImp.CategoryServiceImp;
import com.itnetwork.ServiceImp.NewServiceImp;
import com.itnetwork.Utilities.JsonUtil;
import com.itnetwork.Utilities.PageNewsBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("newsAction")
@Scope("prototype")
public class NewsAtion extends ActionSupport implements ServletRequestAware,ModelDriven<News>{
	private HttpServletRequest request;
	private News news=new News();
	private int categoryId;
	private int start;	//分页使用
	private int limit;
	private ByteArrayInputStream is;
	//private 
	private int categoryid;
	private String jsonContent;//前台的json格式数据
	//spring注入
	private NewServiceImp newServiceImp;//=new NewServiceImp();
	private CategoryServiceImp categoryServiceImp;
	
	public String testParameters()
	{
		Enumeration enumeration = request.getParameterNames();
		while(enumeration.hasMoreElements())
		{
			String paramName=enumeration.nextElement().toString();
			System.out.print(paramName);
			System.out.println("="+request.getParameter(paramName));
		}
		return this.SUCCESS;
	}
	public String newsUpdate(){
		
		System.out.println(jsonContent);
		JSONObject jsonObject = JSONObject.fromObject(jsonContent);
		News news = (News)JSONObject.toBean(jsonObject, News.class);
		System.out.println("cid  "+news.getCid());
		System.out.println("getCtitle  "+news.getCtitle());
		System.out.println("getNclicked   "+news.getNclicked());
		System.out.println("getNcontent   "+news.getNcontent());
		System.out.println("getNhref   "+news.getNhref());
		System.out.println("getNid   "+news.getNid());
		System.out.println("getNsummary   "+news.getNsummary());
		System.out.println("getNtitle    "+news.getNtitle());
		System.out.println("getNtoplevel  "+news.getNtoplevel());
		System.out.println("getNwriter     "+news.getNwriter());
		System.out.println("getCategory    "+news.getCategory());
		System.out.println("getNdate    "+news.getNdate());
		jsonContent="{'success':true,msg:'ok'}";
		is=new ByteArrayInputStream(jsonContent.getBytes()); 
		return this.SUCCESS;
		
	}
//获得前台store与分页的数据，
//一访问此方法就通过newsList.jsp页面输出json格式字符串；
	public String newsList()
	{
		//System.out.println("start="+start);
		//System.out.println("limit="+limit);
		//jsonContent="{'data':[{'cid':13,'ctitle':'category11','nclicked':0,'ncontent':'','ndate':'2011-03-09','nhref':'http://localhost:8080/itnetwork/news0.html','nid':1,'nsummary':'','ntitle':'news0','ntoplevel':0,'nvisiable':false,'nwriter':'writer0'},{'cid':13,'ctitle':'category11','nclicked':0,'ncontent':'','ndate':null,'nhref':'http://localhost:8080/itnetwork/news1.html','nid':2,'nsummary':'','ntitle':'news1','ntoplevel':0,'nvisiable':false,'nwriter':'writer1'},{'cid':13,'ctitle':'category11','nclicked':0,'ncontent':'','ndate':null,'nhref':'http://localhost:8080/itnetwork/news2.html','nid':3,'nsummary':'','ntitle':'news2','ntoplevel':0,'nvisiable':false,'nwriter':'writer2'},{'cid':13,'ctitle':'category11','nclicked':0,'ncontent':'','ndate':null,'nhref':'http://localhost:8080/itnetwork/news3.html','nid':4,'nsummary':'','ntitle':'news3','ntoplevel':0,'nvisiable':false,'nwriter':'writer3'},{'cid':13,'ctitle':'category11','nclicked':0,'ncontent':'','ndate':null,'nhref':'http://localhost:8080/itnetwork/news4.html','nid':5,'nsummary':'','ntitle':'news4','ntoplevel':0,'nvisiable':false,'nwriter':'writer4'}],'totalProperty':26}";
		//return "success";
		Enumeration enumeration = request.getParameterNames();
		while(enumeration.hasMoreElements())
		{
			Object o=enumeration.nextElement();
			System.out.println(o.getClass());
			String paramName=o.toString();
			System.out.print(paramName);
			System.out.println("="+request.getParameter(paramName));
			
		}
		//System.out.println("jieshou cid="+categoryid);
		//System.out.println("jieshou start="+start);
		//System.out.println("jieshou limit="+limit);
		
		
		List<News> newslist = new ArrayList<News>();
		
		if(limit!=0)//如果传来的limit为0，就表示出错吧~测试的时候记得注释这里
		{
			newslist = newServiceImp.getNewsList(start,limit, categoryid);//cid为0表示查询所有new
			newslist = newServiceImp.dealwithNewsO(newslist);//为了暂时处理该对象newslist转换为json对象时，报newlist内嵌对象回环错误
			//newslist.get(0).setNdate(new Date());
			int totalPeoperty=newServiceImp.getCount(categoryid);
			PageNewsBean pageNewsBean = new PageNewsBean(totalPeoperty, newslist);//封装分页bean
			JSONObject jsonarray = JSONObject.fromObject(pageNewsBean,JsonUtil.configJson("yyyy-MM-dd"));
			jsonContent = jsonarray.toString();//.replaceAll("\"", "'");
			System.out.println(jsonContent);
		}
		else
		{
			System.out.println("limit==0");
		}
		
		is=new ByteArrayInputStream(jsonContent.getBytes());  
		
		return this.SUCCESS;
	}

	public String execute()
	{
		//HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get("Request");
		//System.out.println(request.getParameter("news.NId"));
		if(news.getNcontent()!=null)
		{
			System.out.println("categoryId="+categoryId);
			//Category category=new Category();
			//category.setCTitle("categoryTitle");
			/*
			 * 暂时未解决这个传参数的问题，request可以
			 * 
			 */
			if(this.categoryId==0)
			{
				this.categoryId=1;
			}
			
			Category category=categoryServiceImp.getCategoryInfo(categoryId);
			news.setCategory(category);
			//String phyPath=request.getContextPath();
			String tempPath=String.valueOf(new Date().getTime());
			String xslPath=request.getRealPath("news");
			System.out.println("xslPath:"+xslPath);
			//各自栏目下的新闻
			tempPath=xslPath+"\\"+category.getCtitle()+"\\"+tempPath;
			
			xslPath=xslPath+"\\new.xsl";
			news.setNhref(tempPath+".html");
			try {
				if(newServiceImp!=null){
					System.out.println("newServiceImp not null");
				newServiceImp.writeToHtml(xslPath, tempPath, news);
				}else{
					System.out.println("newServiceImp null");
				}
			} catch (IllegalArgumentException e) {   //异常未被处理
				e.printStackTrace();
			} catch (TransformerException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			//newServiceImp.insertNew(news);
		}else{
			System.out.println(news.getNcontent());
		}
		return this.SUCCESS;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public int getCategoryId() {
		return categoryId;
	}
	
	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
		
	}
	@Override
	public News getModel() {
		
		return this.news;
	}

	public NewServiceImp getNewServiceImp() {
		return newServiceImp;
	}
	@Resource(name="newServiceImp")
	public void setNewServiceImp(NewServiceImp newServiceImp) {
		this.newServiceImp = newServiceImp;
	}

	public CategoryServiceImp getCategoryServiceImp() {
		return categoryServiceImp;
	}
	@Resource(name="categoryServiceImp")
	public void setCategoryServiceImp(CategoryServiceImp categoryServiceImp) {
		this.categoryServiceImp = categoryServiceImp;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}


	public int getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(int categoryid) {
		this.categoryid = categoryid;
	}

	public String getJsonContent() {
		return jsonContent;
	}

	public void setJsonContent(String jsonContent) {
		this.jsonContent = jsonContent;
	}
	public ByteArrayInputStream getIs() {
		return is;
	}
	public void setIs(ByteArrayInputStream is) {
		this.is = is;
	}
}
